<?php
/*
Plugin Name: Flickr Image Widget
Plugin URI: http://coloradowebmarketing.com/
Description: Image Widget
Author: James Sweeney III
Version: 1.1
Author URI: http://coloradowebmarketing.com/
*/

require_once 'class/flickr_image.php';

class image_widget extends WP_Widget {
	
	public $flickr_widget;
	
     function image_widget() {
       // Instantiate the parent object
       parent::WP_Widget(false, 'Flickr Random Image');
       $this->flickr_widget = new flickrAPI();
     }

     function widget($args, $instance) {
       // Widget output
       switch($instance['direction']){
       	case 'width':
       		$attribute = 'width:' . $instance['img_size'] . 'px;';
       		break;
		case 'height':
       		$attribute = 'height:' . $instance['img_size'] . 'px;';
       		break;
		default:
			$attribute = 'width:' . $instance['img_size'] . 'px;';
			break;
       }
       ?>
   	
	<script type="text/javascript">
		var postClick()
			jQuery.post(
			   ajaxurl, 
			   {
			      'action':'get_photo',
			      'data':'test'
			   }, 
			   function(response){
			   	document.getElementById('flkr_img').innerHTML = response.text;  
			   }
			);   	
	</script>
       <?php 
       $photo_id = $this->flickr_widget->getRandomPhoto();
       $photo = new flickrPhoto($photo_id);
       $photo->display($attribute);
     }

     function update($new_instance, $old_instance) {
       // Save widget options
       	$instance = $old_instance;
       	
		/* Strip tags (if needed) and update the widget settings. */
       	if(intval($new_instance['img_size']) != 0){
			$instance['img_size'] = strip_tags( $new_instance['img_size'] );
			$instance['direction'] = strip_tags( $new_instance['direction'] );
       	}else{
       		$instance['img_size'] = strip_tags( "200" );
			$instance['direction'] = strip_tags( $new_instance['direction'] );
       	}

		return $instance;
     }

     function form($instance) {
       // Output admin widget options form
       ?>
          <script src="<?php bloginfo('template_url'); ?>../../plugins/js/yui/build/yui/yui-min.js"></script> 
      <?php
      $size_label = '<label>Image Size (pixels): </label>';
      $size = '<input type="text" maxlength="3" style="width:80px" name="'. $this->get_field_name( 'img_size' ) .'" id="'. $this->get_field_id( 'img_size' ) .'" 
      				value="'. $instance['img_size'] .'" /><br/>';
	  
      switch($instance['direction']){
       	case 'width':
       		$options = '<option value="width" selected="selected"> Width </option><option value="height"> Height </option>';
       		break;
		case 'height':
       		$options = '<option value="width"> Width </option><option value="height" selected="selected"> Height </option>';
       		break;
       	default: //width
       		$options = '<option value="width" selected="selected"> Width </option><option value="height"> Height </option>';
       		break;
       }
       $direction_label = "Size Direction: "; 
       $direction = "<select name=\"" . $this->get_field_name( 'direction' ) . "\" id=\"" . $this->get_field_id( 'direction' ) . "\" style=\"width:80px\">$options</select>";
       echo "<table><tr><td>$size_label</td><td>$size</td></tr><tr><td>$direction_label</td><td>$direction</td></tr></table>";
     }
}

function register_widgets ()
{
	register_widget('image_widget');
}

add_action ('widgets_init', 'register_widgets');



?>